<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 头部的布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tuzi" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部布局结束 -->

        <!-- 主体布局 -->
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 使用插件在el-tree内容自定义内容 -->
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-ptess-escape="false"
        @close="hClose"
      >
        <!-- 新增子部门或编辑部门的子组件 -->
        <!-- 解决数据不更新的方式 -
          v-if="showDialog"打开弹层创建组件,关闭弹层销毁组件
          缺点:频繁创建和销毁会影响性能
         -->
        <addOrEdit
          :id="curId"
          ref="myDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="showDialog=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDiaiog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制弹层的显示和隐藏
      curId: '', // 父级的id
      isEdit: false // false新增 || true编辑
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 清空表单
    hClose() {
      console.log('弹层关闭')
      this.$refs.myDept.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确定删除此部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        const res = await delDepartment(id)
        console.log(res, 'delDepartment')
        if (res.data.success) {
          // 重新刷新页面
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
    // 关闭弹层,重置页面
      this.showDialog = false
      this.loadDepartments()
    },
    // 编辑
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 将编辑状态改为true 确定是编辑操作
      this.showDialog = true
      // 解决数据不更新的方法2,在父组化中调用子组件的方法
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    // 新增
    hAdd(id) {
      this.curId = id
      this.isEdit = false // 将编辑的状态改为false 新增操作
      this.showDialog = true
    },
    // 发送请求 获取数据
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res.data.data.depts, 'res.data.data.depts')
        this.originList = res.data.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList)
        console.log(res, 'getDepartments')
        this.list = tranListToTreeList(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
